#include "JsonWrapper.h"
#include <wx/stdpaths.h>

int SortByMemUse(SysResource** Res1, SysResource** Res2)
{
	float MemFree1 = 0, MemFree2 = 0;

	MemFree1 = wxAtof((*Res1)->MemFree);
	MemFree2 = wxAtof((*Res2)->MemFree);
	
	if( MemFree1*10 > MemFree2*10 ) return 1;
	else return 0;

}
int SortByTotalClient(RptTuxClient** Res1, RptTuxClient** Res2)
{
	wxInt32 Client1 = 0, Client2 = 0;

	Client1 = (*Res1)->TotalClient;
	Client2 = (*Res2)->TotalClient;
	
	if( Client1 > Client2 ) return 1;

	else return 0;

}
int SortByTotalQueued(RptTuxQueue** Res1, RptTuxQueue** Res2)
{
	wxInt32 Queued1 = 0, Queued2 = 0;

	Queued1 = (*Res1)->TotalQueued;
	Queued2 = (*Res2)->TotalQueued;
	
	if( Queued1 > Queued2 ) return 1;

	else return 0;

}

int CJsonWrapper::SetReportDir( wxString reportdir)
{
	strReportDir = reportdir;
	return 1;
}

int CJsonWrapper::InitialReportDir( int sitetype )
{
	
enum {TUXEDOSITE, WEBLOGICSITE };
	// copy template into here.
	wxString StdDataDir = wxStandardPaths::Get().GetDataDir();

	wxString strSourcefile;
	if( sitetype == TUXEDOSITE ) 
		strSourcefile = StdDataDir + wxT("\\template\\site\\tux"); 
	if( sitetype == WEBLOGICSITE )
		strSourcefile = StdDataDir + wxT("\\template\\site\\wls"); 

	wxString strTmpShell =
		wxT("start /min xcopy /S /Y \"")  + strSourcefile + wxT("\"  \"") + strReportDir + wxT("\"");

	wxShell( strTmpShell );

	return 1;
}

CJsonWrapper::CJsonWrapper(void)
{
}

CJsonWrapper::~CJsonWrapper(void)
{
}

int CJsonWrapper::DrawCPUData(SysResourceArray sysresarray)
{
	wxString strLineContent, strCount;
	wxInt32 nLineCount = 0, nOffset = 0, nStep = 0, nLoop = 0;
	long nRecCount = 0;
	SysResource sysres;

	wxString StdDataDir = wxStandardPaths::Get().GetDataDir();
	wxString cpuTempfile = StdDataDir + wxT("\\template\\data\\CPU.dat"); 
	wxString cpuChartfile = strReportDir + wxT("\\chart\\data\\CPU_USAGE.dat"); 

	nLineCount = OpenChartTemplate( cpuTempfile );
	if ( nLineCount < 0 ) return -1;

	wxTextFile *txtFile = new wxTextFile( cpuChartfile );
	if ( !txtFile->Exists() ) {
		txtFile->Create( );
	}
	txtFile->Clear();
	txtFile->Write();

	if( !txtFile->Open() ) {
		return -1;
	}

	for( int i = 0; i < nLineCount; i++ ) {
		strLineContent = getTemplateLine( i );
		if( strLineContent.Contains(wxT("<VALUE/")) ) { //fill in Cpu Usage Value
			nOffset = strLineContent.Find( wxT("/") ) ;			
			strCount = strLineContent.Mid( nOffset+1, strLineContent.Find(wxT(">")) - nOffset -1 );
			strCount.ToLong( &nRecCount);
			nStep = sysresarray.GetCount() / nRecCount;
			if( nStep == 0 ) nStep = 1;
			for( int j = 0; j < sysresarray.GetCount(); j += nStep ) {
				sysres = sysresarray.Item(j);
				wxString strLineRecValue = wxString::Format("%s", sysres.CPUIdle );
				if( j + nStep < sysresarray.GetCount() ) 
					strLineRecValue += wxT(",");
				txtFile->AddLine( strLineRecValue );
			}
		} else if( strLineContent.Contains(wxT("<TIME/")) ) { // fill in Record Time
			nOffset = strLineContent.Find( wxT("/") ) ;			
			strCount = strLineContent.Mid( nOffset+1, strLineContent.Find(wxT(">")) - nOffset -1 );
			strCount.ToLong( &nRecCount);
			nStep = sysresarray.GetCount() / nRecCount;
			if( nStep == 0 ) nStep = 1;
			for( int j = 0; j < sysresarray.GetCount(); j += nStep ) {
				sysres = sysresarray.Item(j);
				wxString strLineRecValue = wxString::Format("\"%s\"", sysres.RecTime );
				if( j+ nStep < sysresarray.GetCount() ) 
					strLineRecValue += wxT(",");
				txtFile->AddLine( strLineRecValue );
			}

		} else { //normal line
			txtFile->AddLine( strLineContent );
		}
	}

	CloseChartTemplate();

	txtFile->Write();
	txtFile->Close();
	delete txtFile;

	return 1;
}



int CJsonWrapper::DrawMEMData(SysResourceArray sysresarray)
{
	float fMemMax = 0, fMemMin = 0,fRate = 0;
	wxString strLineContent, strCount;
	wxString strLineRecValue;
	wxInt32 nLineCount = 0, nOffset = 0, nStep = 0;
	long nRecCount = 0;
	SysResource sysres;


	wxString StdDataDir = wxStandardPaths::Get().GetDataDir();
	wxString memTempfile = StdDataDir + wxT("\\template\\data\\MEM.dat"); 
	wxString memChartfile = strReportDir + wxT("\\chart\\data\\MEM_USAGE.dat"); 

	nLineCount = OpenChartTemplate( memTempfile );
	if ( nLineCount < 0 ) return -1;

	wxTextFile *txtFile = new wxTextFile( memChartfile );
	if ( !txtFile->Exists() ) {
		txtFile->Create( );
	}
	txtFile->Clear();
	txtFile->Write();

	if( !txtFile->Open() ) {
		return -1;
	}

	for( int i = 0; i < nLineCount; i++ ) {
		strLineContent = getTemplateLine( i );
		if( strLineContent.Contains(wxT("<YAXIS>")) ) { //fill in Cpu Usage Value
			SysResourceArray sysressortarray = sysresarray;
			sysressortarray.Sort(SortByMemUse);

			fMemMax = wxAtof( sysressortarray.Item(sysressortarray.GetCount()-1).MemFree );
			fMemMin = wxAtof( sysressortarray.Item(0).MemFree );
			fRate = (fMemMax - fMemMin ) / 8;
			strLineRecValue = wxString::Format( wxT("\"min\": %4.2f,\n\"max\": %4.2f,\n"), fMemMin - 1.5*fRate, fMemMax + 1.5*fRate);
			strLineRecValue += wxString::Format("\"steps\":%4.2f\n", fRate);

			txtFile->AddLine( strLineRecValue );
		} else if( strLineContent.Contains(wxT("<VALUE/")) ) { //fill in Cpu Usage Value
			nOffset = strLineContent.Find( wxT("/") ) ;			
			strCount = strLineContent.Mid( nOffset+1, strLineContent.Find(wxT(">")) - nOffset -1 );
			strCount.ToLong( &nRecCount);
			nStep = sysresarray.GetCount() / nRecCount;
			if( nStep == 0 ) nStep = 1;
			for( int j = 0; j < sysresarray.GetCount(); j += nStep ) {
				sysres = sysresarray.Item(j);
				wxString strLineRecValue = wxString::Format("%s", sysres.MemFree );
				if( j + nStep < sysresarray.GetCount() ) 
					strLineRecValue += wxT(",");
				txtFile->AddLine( strLineRecValue );
			}
		} else if( strLineContent.Contains(wxT("<TIME/")) ) { // fill in Record Time
			nOffset = strLineContent.Find( wxT("/") ) ;			
			strCount = strLineContent.Mid( nOffset+1, strLineContent.Find(wxT(">")) - nOffset -1 );
			strCount.ToLong( &nRecCount);
			nStep = sysresarray.GetCount() / nRecCount;
			if( nStep == 0 ) nStep = 1;
			for( int j = 0; j < sysresarray.GetCount(); j += nStep ) {
				sysres = sysresarray.Item(j);
				wxString strLineRecValue = wxString::Format("\"%s\"", sysres.RecTime );
				if( j+ nStep < sysresarray.GetCount() ) 
					strLineRecValue += wxT(",");
				txtFile->AddLine( strLineRecValue );
			}
		} else { //normal line
			txtFile->AddLine( strLineContent );
		}
	}

	CloseChartTemplate();

	txtFile->Write();
	txtFile->Close();
	delete txtFile;

	return 1;
}


int CJsonWrapper::DrawTuxClientData( RptTuxClientArray rpttuxcltarray )
{

	wxInt32 iHighClient = 0,iLowClient = 0, iRate = 0;
	wxInt32 iLowMark = 0, iHighMark = 0;
	wxString strLineContent, strCount;
	wxString strLineRecValue;
	wxInt32 nLineCount = 0, nOffset = 0, nStep = 0;
	long nRecCount = 0;
	RptTuxClient rpttuxclt;


	wxString StdDataDir = wxStandardPaths::Get().GetDataDir();
	wxString memTempfile = StdDataDir + wxT("\\template\\data\\TUX_CLIENT.dat"); 
	wxString memChartfile = strReportDir + wxT("\\chart\\data\\CLIENT_USAGE.dat"); 

	nLineCount = OpenChartTemplate( memTempfile );
	if ( nLineCount < 0 ) return -1;

	wxTextFile *txtFile = new wxTextFile( memChartfile );
	if ( !txtFile->Exists() ) {
		txtFile->Create( );
	}
	txtFile->Clear();
	txtFile->Write();

	if( !txtFile->Open() ) {
		return -1;
	}

	for( int i = 0; i < nLineCount; i++ ) {
		strLineContent = getTemplateLine( i );
		if( strLineContent.Contains(wxT("<YAXIS>")) ) { //fill in Client Usage Value
			RptTuxClientArray rpttuxcltsortarray = rpttuxcltarray;

			rpttuxcltsortarray.Sort(SortByTotalClient);

			iHighClient = rpttuxcltsortarray.Item(rpttuxcltsortarray.GetCount()-1).TotalClient;
			iLowClient =  rpttuxcltsortarray.Item(0).BusyClient;
			iRate = (iHighClient - iLowClient ) / 8;
			
			if( iRate == 0 ) iRate = 1;
			wxInt32 iLowMark = 0;
			if( iLowClient < 10  ) { 
				iLowMark = 0;
			} else {
				iLowMark = iLowClient - 2*iRate;
			}
			strLineRecValue = wxString::Format( wxT("\"min\": %d,\n\"max\": %d,\n"), iLowMark, iHighClient + 2*iRate);
	
			strLineRecValue += wxString::Format("\"steps\":%d", iRate);

			txtFile->AddLine( strLineRecValue );
		} else if( strLineContent.Contains(wxT("<BUSYCLIENT/")) ) { //fill in BusyClient Value
			nOffset = strLineContent.Find( wxT("/") ) ;			
			strCount = strLineContent.Mid( nOffset+1, strLineContent.Find(wxT(">")) - nOffset -1 );
			strCount.ToLong( &nRecCount);
			nStep = rpttuxcltarray.GetCount() / nRecCount;
			if( nStep == 0 ) nStep = 1;
			for( int j = 0; j < rpttuxcltarray.GetCount(); j += nStep ) {
				rpttuxclt = rpttuxcltarray.Item(j);
				wxString strLineRecValue = wxString::Format("%d", rpttuxclt.BusyClient );
				if( j + nStep < rpttuxcltarray.GetCount() ) 
					strLineRecValue += wxT(",");
				txtFile->AddLine( strLineRecValue );
			}
		} else if( strLineContent.Contains(wxT("<TOTALCLIENT/")) ) { //fill in BusyClient Value
			nOffset = strLineContent.Find( wxT("/") ) ;			
			strCount = strLineContent.Mid( nOffset+1, strLineContent.Find(wxT(">")) - nOffset -1 );
			strCount.ToLong( &nRecCount);
			nStep = rpttuxcltarray.GetCount() / nRecCount;
			if( nStep == 0 ) nStep = 1;
			for( int j = 0; j < rpttuxcltarray.GetCount(); j += nStep ) {
				rpttuxclt = rpttuxcltarray.Item(j);
				wxString strLineRecValue = wxString::Format("%d", rpttuxclt.TotalClient );
				if( j + nStep < rpttuxcltarray.GetCount() ) 
					strLineRecValue += wxT(",");
				txtFile->AddLine( strLineRecValue );
			}
		} else if( strLineContent.Contains(wxT("<TIME/")) ) { // fill in Record Time
			nOffset = strLineContent.Find( wxT("/") ) ;			
			strCount = strLineContent.Mid( nOffset+1, strLineContent.Find(wxT(">")) - nOffset -1 );
			strCount.ToLong( &nRecCount);
			nStep = rpttuxcltarray.GetCount() / nRecCount;
			if( nStep == 0 ) nStep = 1;
			for( int j = 0; j < rpttuxcltarray.GetCount(); j += nStep ) {
				rpttuxclt = rpttuxcltarray.Item(j);
				wxString strLineRecValue = wxString::Format("\"%s\"", rpttuxclt.RecTime );
				if( j+ nStep < rpttuxcltarray.GetCount() ) 
					strLineRecValue += wxT(",");
				txtFile->AddLine( strLineRecValue );
			}
		} else { //normal line
			txtFile->AddLine( strLineContent );
		}
	}

	CloseChartTemplate();

	txtFile->Write();
	txtFile->Close();
	delete txtFile;

	return 1;
}

int CJsonWrapper::DrawTuxQueueData( RptTuxQueueArray rpttuxquearray )
{
	wxInt32 iHighQueued = 0,iLowQueued = 0, iRate = 0;
	wxInt32 iLowMark = 0, iHighMark = 0;
	wxString strLineContent, strCount;
	wxString strLineRecValue;
	wxInt32 nLineCount = 0, nOffset = 0, nStep = 0;
	long nRecCount = 0;
	RptTuxQueue rpttuxque;


	wxString StdDataDir = wxStandardPaths::Get().GetDataDir();
	wxString memTempfile = StdDataDir + wxT("\\template\\data\\TUX_QUEUE.dat"); 
	wxString memChartfile = strReportDir + wxT("\\chart\\data\\QUEUE_USAGE.dat"); 

	nLineCount = OpenChartTemplate( memTempfile );
	if ( nLineCount < 0 ) return -1;

	wxTextFile *txtFile = new wxTextFile( memChartfile );
	if ( !txtFile->Exists() ) {
		txtFile->Create( );
	}
	txtFile->Clear();
	txtFile->Write();

	if( !txtFile->Open() ) {
		return -1;
	}

	for( int i = 0; i < nLineCount; i++ ) {
		strLineContent = getTemplateLine( i );
		if( strLineContent.Contains(wxT("<YAXIS>")) ) { //fill in Client Usage Value
			RptTuxQueueArray rpttuxquesortarray = rpttuxquearray;

			rpttuxquesortarray.Sort(SortByTotalQueued);

			iHighQueued = rpttuxquesortarray.Item(rpttuxquesortarray.GetCount()-1).TotalQueued;
			iLowQueued =  rpttuxquesortarray.Item(0).TotalQueued;
			iRate = (iHighQueued - iLowQueued ) / 8;
			
			if( iRate == 0 ) iRate = 1;
			wxInt32 iLowMark = 0;
			if( iLowQueued < 10  ) { 
				iLowMark = 0;
			} else {
				iLowMark = iLowQueued - 2*iRate;
			}
			strLineRecValue = wxString::Format( wxT("\"min\": %d,\n\"max\": %d,\n"), iLowMark, iHighQueued + 2*iRate);
	
			strLineRecValue += wxString::Format("\"steps\":%d", iRate);

			txtFile->AddLine( strLineRecValue );
		}  else if( strLineContent.Contains(wxT("<TOTALQUEUED/")) ) { //fill in BusyClient Value
			nOffset = strLineContent.Find( wxT("/") ) ;			
			strCount = strLineContent.Mid( nOffset+1, strLineContent.Find(wxT(">")) - nOffset -1 );
			strCount.ToLong( &nRecCount);
			nStep = rpttuxquearray.GetCount() / nRecCount;
			if( nStep == 0 ) nStep = 1;
			for( int j = 0; j < rpttuxquearray.GetCount(); j += nStep ) {
				rpttuxque = rpttuxquearray.Item(j);
				wxString strLineRecValue = wxString::Format("%d", rpttuxque.TotalQueued );
				if( j + nStep < rpttuxquearray.GetCount() ) 
					strLineRecValue += wxT(",");
				txtFile->AddLine( strLineRecValue );
			}
		} else if( strLineContent.Contains(wxT("<TIME/")) ) { // fill in Record Time
			nOffset = strLineContent.Find( wxT("/") ) ;			
			strCount = strLineContent.Mid( nOffset+1, strLineContent.Find(wxT(">")) - nOffset -1 );
			strCount.ToLong( &nRecCount);
			nStep = rpttuxquearray.GetCount() / nRecCount;
			if( nStep == 0 ) nStep = 1;
			for( int j = 0; j < rpttuxquearray.GetCount(); j += nStep ) {
				rpttuxque = rpttuxquearray.Item(j);
				wxString strLineRecValue = wxString::Format("\"%s\"", rpttuxque.RecTime );
				if( j+ nStep < rpttuxquearray.GetCount() ) 
					strLineRecValue += wxT(",");
				txtFile->AddLine( strLineRecValue );
			}
		} else { //normal line
			txtFile->AddLine( strLineContent );
		}
	}

	CloseChartTemplate();

	txtFile->Write();
	txtFile->Close();
	delete txtFile;

	return 1;

}
int CJsonWrapper::OpenChartTemplate( wxString filename )
{
	if( ! m_TemplateFile.Open( filename ) ) return -1;
	int nCount = m_TemplateFile.GetLineCount();
	
	return nCount;
}

int CJsonWrapper::CloseChartTemplate( )
{
	if ( m_TemplateFile.IsOpened() )
		m_TemplateFile.Close();

	return 1;
}

wxString CJsonWrapper::getTemplateLine( int linenumber )
{
	wxString strLineContent;
	strLineContent.Empty();

	if ( m_TemplateFile.IsOpened() ) {
		strLineContent = m_TemplateFile.GetLine( linenumber );
	}

	return strLineContent;
}